<?php

/**
 * This is the model class for table "{{desa}}".
 *
 * The followings are the available columns in table '{{desa}}':
 * @property integer $id_desa
 * @property string $nama_desa
 * @property integer $id_kelurahan
 *
 * The followings are the available model relations:
 * @property Kelurahan $idKelurahan
 */
class Desa extends CActiveRecord
{
    public $nama_kecamatan;
    public $nama_kelurahan;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Desa the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{desa}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_kelurahan', 'required'),
			array('id_kelurahan', 'numerical', 'integerOnly'=>true),
			array('nama_desa', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_desa, nama_desa, id_kelurahan,nama_kecamatan, nama_kelurahan', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idKelurahan' => array(self::BELONGS_TO, 'Kelurahan', 'id_kelurahan'),
  	        'desaAnggotas' => array(self::HAS_MANY, 'DesaAnggota', 'id_desa'),
  	        'idKecamatan' => array(self::HAS_ONE, 'Kecamatan', 'id_kecamatan',
            'through'=>'idKelurahan'
            ),

		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_desa' => 'Id Desa',
			'nama_desa' => 'Nama Desa',
			'id_kelurahan' => 'Id Kelurahan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_desa',$this->id_desa);
		$criteria->compare('nama_desa',$this->nama_desa,true);
		$criteria->compare('id_kelurahan',$this->id_kelurahan);
//       $criteria->join = 'LEFT JOIN {{kelurahan}} l ON t.id_kelurahan=l.id_kelurahan
//                          LEFT JOIN {{kecamatan}} c ON l.id_kecamatan=c.id_kecamatan';
       $criteria->with = array('idKelurahan','idKecamatan');
       $criteria->compare('nama_kecamatan', $this->nama_kecamatan, true);
       $criteria->compare('nama_kelurahan', $this->nama_kelurahan, true);
       
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}